package com.alibaba.excel.convert;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.domain.Dict;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.support.ExcelDictContextHolder;
import com.alibaba.excel.util.StringUtils;
import com.alibaba.excel.util.TypeUtils;
import jdk.nashorn.internal.ir.IfNode;
import lombok.AllArgsConstructor;
import org.ehcache.impl.internal.classes.commonslang.ArrayUtils;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @author lxk
 * @date 2022/1/18
 * @description
 */
@AllArgsConstructor
public class ExcelDictConvert implements Converter<Object> {

    private String dictType;

    /**
     * excel转java
     *
     * @param cellData
     * @param contentProperty
     * @param globalConfiguration
     * @return
     * @throws Exception
     */
    @Override
    public Object convertToJavaData(ReadCellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        String stringValue;
        switch (cellData.getType()) {
            case BOOLEAN:
                if (cellData.getBooleanValue() == null) {
                    return null;
                }
                stringValue = cellData.getBooleanValue().toString();
                break;
            case NUMBER:
                if (cellData.getNumberValue() == null) {
                    return null;
                }
                stringValue = cellData.getNumberValue().toString();
                break;
            default:
                stringValue = cellData.getStringValue();
                if (StringUtils.isBlank(stringValue)) {
                    return null;
                }
        }
        Field field = contentProperty.getField();
        List<Dict> dictDatas = ExcelDictContextHolder.getByDictType(dictType);
        if (dictDatas == null){
            return stringValue;
        }
        Optional<Dict> optional = dictDatas.stream().filter(data -> data.getDictLabel().equals(stringValue)).findFirst();
        if (optional.isPresent()){
            return TypeUtils.cast(optional.get().getDictValue(), field.getType());
        }
        return stringValue ;
    }

    /**
     * java转excel
     *
     * @param value
     * @param contentProperty
     * @param globalConfiguration
     * @return
     * @throws Exception
     */
    @Override
    public WriteCellData<?> convertToExcelData(Object value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        if (value == null) {
            return new WriteCellData();
        }
        String val = TypeUtils.castToString(value);
        List<Dict> dictDatas = ExcelDictContextHolder.getByDictType(dictType);
        Optional<Dict> optional = dictDatas.stream().filter(data -> data.getDictValue().equals(val)).findFirst();
        return optional.map(dict -> new WriteCellData(dict.getDictLabel())).orElseGet(() -> new WriteCellData(value.toString()));
    }


}
